package com.itheima.tlias84.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias84.dto.SelectClazzAllDTO;
import com.itheima.tlias84.dto.SelectClazzDTO;
import com.itheima.tlias84.dto.SelectClazzDetailDTO;
import com.itheima.tlias84.mapper.ClazzMapper;
import com.itheima.tlias84.po.InsertClazzParam;
import com.itheima.tlias84.po.SelectClazzPageParam;
import com.itheima.tlias84.po.UpdateClazzParam;
import com.itheima.tlias84.service.ClazzService;
import com.itheima.tlias84.utils.ResultUtil;
import com.itheima.tlias84.vo.PageVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 班级服务接口实现类
 */
@Service
public class ClazzServiceImpl implements ClazzService {
    @Resource
    ClazzMapper clazzMapper;

    /**
     * 添加班级
     * @param param 添加班级参数
     * @return 添加数量
     */
    @Override
    public int insertClazz(InsertClazzParam param) {
        //设置时间和状态
        param.setCreateTime(LocalDateTime.now());
        param.setUpdateTime(LocalDateTime.now());
        param.setState(0);
        return clazzMapper.insertClazz(param);
    }

    /**
     * 分页查询班级
     * @param param 模糊查询班级参数
     * @return 查询结果
     */
    @Override
    public ResultUtil selectClazzByPage(SelectClazzPageParam param) {
        //设置分页
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectClazzDTO> dtos = clazzMapper.selectClazzByPage(param);
        PageVO<SelectClazzDTO> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(dtos);
        return ResultUtil.success(pageVO);
    }


    /**
     * 修改更新班级
     * @param param 更新班级参数
     * @return 数据库受影响行数
     */
    @Override
    public int updateClazz(UpdateClazzParam param) {
        //更新时间
        param.setUpdateTime(LocalDateTime.now());
        param.setState(0);
        //更新数据
        return clazzMapper.updateClazz(param);
    }

    /**
     * 根据id查询班级详情
     * @param id 班级id
     * @return 详情
     */
    @Override
    public SelectClazzDetailDTO selectClazzById(Integer id) {
        return clazzMapper.selectClazzById(id);
    }

    /**
     * 查询所有班级
     * @return 所有班级
     */
    @Override
    public List<SelectClazzAllDTO> selectClazzAll() {
        List<SelectClazzAllDTO> dtos = clazzMapper.selectClazzAll();
        return dtos;
    }

    /**
     * TODO 判断是否有学生，删除班级
     * @param id 班级id
     * @return 删除数量
     */
    @Override
    public int deleteById(Integer id) {
        //TODO 判断是否有学生
        return clazzMapper.deleteById(id);
    }
}
